package com.example.knowman.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.knowman.ObService.OBKnowCategoryService;
import com.example.knowman.ObService.OBKnowMainService;
import com.example.knowman.ObService.OBknowLabelDetailService;
import com.example.knowman.ObService.OBlabelCategoryService;
import com.example.knowman.ReturnMessage.Message;
import com.example.knowman.dao.knowLabelDetailEx;
import com.example.knowman.dao.knowLabelMainEx;
import com.example.knowman.entity.knowLabelDetail;
import com.example.knowman.entity.knowLabelMain;
import com.example.knowman.entity.knowMain;
import com.example.knowman.entity.labelCategory;
import com.example.knowman.mapper.KnowLabelDetailMapper;
import com.example.knowman.mapper.KnowLabelMainMapper;
import com.example.knowman.mapper.LabelCategoryMapper;
import com.example.knowman.mapper.StaffListMapper;
import com.example.knowman.service.IKnowCategoryService;
import com.example.knowman.service.IStaffListService;
import com.example.knowman.service.IknowLabelDetailService;
import com.example.knowman.service.IknowLabelMainService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class KnowLabelMainServiceImpl extends ServiceImpl<KnowLabelMainMapper, knowLabelMain> implements IknowLabelMainService {

    private final OBKnowMainService obKnowMainService;

    private final OBlabelCategoryService oblabelCategoryService;

    private final OBKnowCategoryService obKnowCategoryService;

    public KnowLabelMainServiceImpl(OBKnowMainService obKnowMainService,OBlabelCategoryService oblabelCategoryService,OBKnowCategoryService obKnowCategoryService) {
        this.obKnowMainService = obKnowMainService;
        this.oblabelCategoryService = oblabelCategoryService;
        this.obKnowCategoryService = obKnowCategoryService;
    }
    @Override
    public Message updateByknowLblId(Integer knowLblId, String lblContent, String notes) {
        Message message = new Message();
        this.lambdaUpdate().eq(knowLabelMain::getKnowLblId,knowLblId)
                .set(lblContent != null && !lblContent.isEmpty(),knowLabelMain::getLblContent,lblContent)
                .set(notes != null && !notes.isEmpty(),knowLabelMain::getNotes,notes)
                .update();

        message.setCode(200);
        message.setTip("修改成功");

        return message;
    }

    @Override
    public knowLabelMain getSingle(Integer knowId) {
        return this.lambdaQuery().select().eq(knowLabelMain::getKnowId,knowId).one();
    }

    @Override
    public Message Insert(Integer knowId, String lblContent, String staffCode, String notes) {
        Message message = new Message();

        knowLabelMain temp = new knowLabelMain();
        temp.setKnowId(knowId);
        temp.setLblContent(lblContent);
        temp.setStaffCode(staffCode);
        temp.setNotes(notes);

        try{
            this.save(temp);
            message.setCode(200);
            message.setTip("新增成功");
        }catch (Exception e){
            message.setCode(100);
            message.setTip("新增失败");
        }

        return message;
    }

    @Override
    public knowLabelMainEx getLabelsByKnowId(Integer knowId) {
        Integer knowCateId = obKnowMainService.returnknowCateIdByknowId(knowId);
        String knowCateName = obKnowCategoryService.returnKnowCateNameByKnowCateId(knowCateId);
        List<labelCategory> lis = oblabelCategoryService.getLabelsByKnowCateId(knowCateId);

        knowLabelMainEx res = new knowLabelMainEx();
        res.setKnowCateId(knowCateId);
        res.setKnowCateName(knowCateName);
        res.setList(lis);
        return res;
    }


}
